//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <gdiplus.h>

#include "Unit1.h"
#include "GraphicsHelperFunctions.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{

	ULONG_PTR gdipToken;
	Gdiplus::GdiplusStartupInput gdipStartupInput;
	Gdiplus::GdiplusStartup(&gdipToken, &gdipStartupInput, 0);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

	PanelDest->Repaint();

	String strROP = ComboBox1->Text.SubString(ComboBox1->Text.Pos(L"0x"), 10);
	StretchBlt(PaintBox2->Canvas->Handle, 0, 0, PanelDest->Width, PanelDest->Height, PaintBox1->Canvas->Handle, 0, 0, PanelSrc->Width, PanelSrc->Height, strROP.ToInt());

}
//---------------------------------------------------------------------------
void LoadPicture_Gdiplus(HWND hWndCanvas, HDC hDCCanvas, LPCTSTR lpFileName)
{
	Gdiplus::Graphics *pGraphics = NULL;
	Gdiplus::Image *pImage = NULL;
	LONG lW = 0, lH = 0;

	HDC hDCCanvas1 = (hDCCanvas)?hDCCanvas:GetDC(hWndCanvas);

	pGraphics = new Gdiplus::Graphics(hDCCanvas1);
	pImage = new Gdiplus::Image(lpFileName);

	if (pImage->GetLastStatus() == Gdiplus::Ok)
	{

		lW = pImage->GetWidth();
		lH = pImage->GetHeight();

		Gdiplus::Rect rcImage(0, 0, lW, lH);
		pGraphics->DrawImage(pImage, rcImage);

	}

	delete pImage;
	delete pGraphics;
	if (!hDCCanvas)
		ReleaseDC(hWndCanvas, hDCCanvas1);
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{

	PanelSrc->Repaint();

	Gdiplus::Graphics *pGraphics = NULL;
	Gdiplus::Image *pImage = NULL;

	pGraphics = new Gdiplus::Graphics(PaintBox1->Canvas->Handle);
	pImage = new Gdiplus::Image(L"vb_fo3.png");

	if (pImage->GetLastStatus() == Gdiplus::Ok)
	{

		Gdiplus::Rect rcImage(0, 0, PaintBox1->Width, PaintBox1->Height);
		pGraphics->DrawImage(pImage, rcImage);

	}

	delete pImage;
	delete pGraphics;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{

	PanelDest->Repaint();

	String strROP = ComboBox1->Text.SubString(ComboBox1->Text.Pos(L"0x"), 10);
	BitBlt(PaintBox2->Canvas->Handle, 0, 0, PanelSrc->Width, PanelSrc->Height, PaintBox1->Canvas->Handle, 0, 0, strROP.ToInt());

}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
		  int X, int Y)
{

	TPaintBox *pSrcCanvas = static_cast<TPaintBox*>(Sender);
	DWORD dwBuffSize;
	LPBYTE pBuff;
	LPIMAGEDESCR pIMAGE;

	GetImagePixels(pSrcCanvas->Canvas->Handle, pSrcCanvas->Width, pSrcCanvas->Height, &pBuff, &dwBuffSize, &pIMAGE);

	COLORREF cr = GetPixel(pBuff, pIMAGE, X, Y);
	Panel1->Caption = IntToStr(R_BGRA(cr)) + L" " + IntToStr(G_BGRA(cr)) + L" " + IntToStr(B_BGRA(cr));
	Panel2->Caption = IntToStr(A_BGRA(cr));

}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox2MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y)
{

	TPaintBox *pSrcCanvas = static_cast<TPaintBox*>(Sender);
	DWORD dwBuffSize;
	LPBYTE pBuff;
	LPIMAGEDESCR pIMAGE;

	GetImagePixels(pSrcCanvas->Canvas->Handle, pSrcCanvas->Width, pSrcCanvas->Height, &pBuff, &dwBuffSize, &pIMAGE);

	COLORREF cr = GetPixel(pBuff, pIMAGE, X, Y);
	Panel3->Caption = IntToStr(R_BGRA(cr)) + L" " + IntToStr(G_BGRA(cr)) + L" " + IntToStr(B_BGRA(cr));
	Panel4->Caption = IntToStr(A_BGRA(cr));

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{

	PanelDest->Repaint();

	DWORD dwBuffSize;
	LPBYTE pBuff;
	LPIMAGEDESCR pIMAGE;

	GetImagePixels(PaintBox1->Canvas->Handle, PaintBox1->Width, PaintBox1->Height, &pBuff, &dwBuffSize, &pIMAGE);

	Gdiplus::Graphics *pGraphics = NULL;
	Gdiplus::Bitmap *pBitmap = NULL;

	pGraphics = new Gdiplus::Graphics(PaintBox2->Canvas->Handle);
	pBitmap = new Gdiplus::Bitmap(PaintBox1->Width, PaintBox1->Height, PaintBox1->Width << 2/* * (32 / 8)*/, PixelFormat32bppARGB, pBuff);
	Gdiplus::Rect rcBitmap(0, 0, PaintBox2->Width, PaintBox2->Height);
	pBitmap->RotateFlip(Gdiplus::RotateNoneFlipY);
	pGraphics->DrawImage(pBitmap, rcBitmap);

	delete pGraphics;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)
{

	SetROP2(PaintBox2->Canvas->Handle, (ComboBox2->ItemIndex + 1));

	HPEN hPen = CreatePen(PS_SOLID, 8, RGB(255, 0, 0));
	HPEN hOldPen = static_cast<HPEN>(SelectObject(PaintBox2->Canvas->Handle, hPen));

	MoveToEx(PaintBox2->Canvas->Handle, 20, 20, NULL);
	LineTo(PaintBox2->Canvas->Handle, PaintBox2->Width - 20, PaintBox2->Height - 20);

	DeleteObject(SelectObject(PaintBox2->Canvas->Handle, hOldPen));

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button6Click(TObject *Sender)
{

	SetROP2(PaintBox2->Canvas->Handle, (ComboBox2->ItemIndex + 1));

	HPEN hPen = CreatePen(PS_SOLID, 4, RGB(255, 225, 0));
	HPEN hOldPen = static_cast<HPEN>(SelectObject(PaintBox2->Canvas->Handle, hPen));

	HBRUSH hBrush = CreateSolidBrush(RGB(0, 225, 125));
	HBRUSH hOldBrush = static_cast<HBRUSH>(SelectObject(PaintBox2->Canvas->Handle, hBrush));

	Ellipse(PaintBox2->Canvas->Handle, 20, 20, PaintBox2->Width - 20, PaintBox2->Height - 20);

	DeleteObject(SelectObject(PaintBox2->Canvas->Handle, hOldPen));
	DeleteObject(SelectObject(PaintBox2->Canvas->Handle, hOldBrush));

}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button7Click(TObject *Sender)
{

	HBRUSH hBrush1 = CreateSolidBrush(RGB(125, 200, 0));
	HBRUSH hBrush2 = CreateSolidBrush(RGB(255, 125, 0));

	RECT rcToFill = { 20, 20, PaintBox2->Width - 20, PaintBox2->Height - 20 };

	FillRect(PaintBox2->Canvas->Handle, &rcToFill, hBrush1);
	FrameRect(PaintBox2->Canvas->Handle, &rcToFill, hBrush2);

	DeleteObject(hBrush1);
	DeleteObject(hBrush2);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button9Click(TObject *Sender)
{

	PanelDest->Repaint();

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button8Click(TObject *Sender)
{

	SetROP2(PaintBox2->Canvas->Handle, (ComboBox2->ItemIndex + 1));

	HBRUSH hBrush = CreateSolidBrush(RGB(0, 125, 225));
	HBRUSH hOldBrush = static_cast<HBRUSH>(SelectObject(PaintBox2->Canvas->Handle, hBrush));

	ExtFloodFill(PaintBox2->Canvas->Handle, 1, 1, GetPixel(PaintBox2->Canvas->Handle, 1, 1), FLOODFILLSURFACE);

	DeleteObject(SelectObject(PaintBox2->Canvas->Handle, hOldBrush));

}
//---------------------------------------------------------------------------

